//
//  MYGeneralSuccessToastView.m
//  JQLive
//
//  Created by Bruce on 2020/4/18.
//  Copyright © 2020 JQLive. All rights reserved.
//

#import "MYGeneralSuccessToastView.h"

@interface MYGeneralSuccessToastView ()
{
    UILabel *contentLabel;
}
@property (nonatomic, copy) MYGeneralSuccessToastViewCompletion completion;
@end

@implementation MYGeneralSuccessToastView

- (void)showWithView:(UIView *)view
             content:(NSString *)content
          completion:(MYGeneralSuccessToastViewCompletion)completion
{
    [view addSubview:self];
    
    self.completion = completion;
    contentLabel.text = content;
    
    MY_WEAK;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf dismiss];
    });
}

- (instancetype)initWithFrame:(CGRect)frame
{
    frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    if(self = [super initWithFrame:frame])
    {
        [self layout];
    }
    
    return self;
}

- (void)layout
{
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    
    UIView *my_mainView = [[UIView alloc] init];
    my_mainView.backgroundColor = [UIColor whiteColor];
    my_mainView.clipsToBounds = YES;
    my_mainView.layer.cornerRadius = 8;
    [my_mainView addBouncesAnimation];
    [self addSubview:my_mainView];
    
    UIImageView *iconImgView = [[UIImageView alloc] init];
    iconImgView.image = [UIImage imageNamed:@"ic_login_success2"];
    [my_mainView addSubview:iconImgView];
    
    contentLabel = [[UILabel alloc] init];
    contentLabel.font = [UIFont boldSystemFontOfSize:16];
    contentLabel.textColor = MY_Text_Color_Theme1;
    contentLabel.textAlignment = NSTextAlignmentCenter;
    [my_mainView addSubview:contentLabel];
    
    [my_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(@180);
        make.height.equalTo(@130);
    }];
    
    [iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(my_mainView);
        make.bottom.equalTo(my_mainView.mas_centerY).offset(-8);
    }];
    
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(my_mainView);
        make.top.equalTo(my_mainView.mas_centerY).offset(8);
    }];
}

- (void)dismiss
{
    if (self.completion)
    {
        self.completion(self);
    }
    
    //点击关闭
    [UIView animateWithDuration:0.20 animations:^{
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.20 animations:^{
            self.alpha = 0.0;
        } completion:^(BOOL finished) {
            self.hidden = YES;
            [self removeFromSuperview];
        }];
    }];
}

@end
